#ifndef CCUSTOMBOXVIEW_H_
#define CCUSTOMBOXVIEW_H_

#include <QButtonGroup>
#include <QList>
#include <QMap>
#include <QMutex>
#include <QPushButton>
#include <QVariant>
#include <QWidget>

class CCustomBoxCell : public QPushButton
{
    Q_OBJECT
public:
    enum EDataRole
    {
        EDataRole_Index = 0,
        EDataRole_Item = 1,
        EDataRole_Door = 2
    };

    CCustomBoxCell(QWidget* parent = 0);
    ~CCustomBoxCell();

    inline void SetData(int index, QVariant data)
    {
        data_[index] = data;
        update();
    }
    inline QVariant Data(int index) { return data_[index]; }

protected:
    void paintEvent(QPaintEvent* e) override;

private:
    QMap<int, QVariant> data_;
};

class CCustomBoxView : public QWidget
{
    Q_OBJECT

public:
    CCustomBoxView(QWidget* parent = 0);
    ~CCustomBoxView();

    inline void Reset() { hide_all(); }
    void SetLayout(int max_row, int max_col);
    void SetData(int index, int role, QVariant data);

protected:
    void init();
    void hide_all();
    void resize_btngroup(int32_t size);

    bool eventFilter(QObject* watched, QEvent* event);
    void mousePressEvent(QMouseEvent* e) override;
    void mouseMoveEvent(QMouseEvent* e) override;
    void mouseReleaseEvent(QMouseEvent* e) override;

signals:
    void boxClicked(int id);

private:
    std::vector<CCustomBoxCell*> cells_;
    QButtonGroup* btn_group_;

    bool flag_drag_;
    bool flag_drag_continue_;
    int32_t drag_posx_;
    int32_t drag_posy_;
};

#endif  // CCUSTOMBOXVIEW_H_
